package com.example.demo;

import com.example.demo.inChat.DataBaseServiceImpl;
import com.example.demo.inChat.FromServerServiceImpl;
import com.example.demo.inChat.InitChat;
import com.example.demo.inChat.VerifyServiceImpl;
import com.example.demo.netty.client.NettyClientFilter;

import com.github.unclecatmyself.auto.ConfigFactory;
import com.github.unclecatmyself.auto.InitServer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.lang.reflect.Method;
import java.util.HashMap;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//
@SpringBootApplication
@EnableScheduling
@EnableCaching
@EnableSwagger2
public class GCApplication extends SpringBootServletInitializer {


    public static void main(String[] args) {
        initChat();
        SpringApplication.run(GCApplication.class ,args);
    }

    private static void initChat() {
        //配置你的自定义配置
        ConfigFactory.initNetty = new InitChat();
        //配置校验类
        ConfigFactory.inChatVerifyService = new VerifyServiceImpl();
        //配置消息接收处理类
        ConfigFactory.inChatToDataBaseService = new DataBaseServiceImpl();
        //配置服务端系统消息枚举，这里的值无所谓 TYPE1或者TYPE2或者TYPE-N均可以
        ConfigFactory.fromServerService = FromServerServiceImpl.TYPE1;
        //配置分布式Redis地址，端口目前默认的，下一版可以支持修改。
        ConfigFactory.RedisIP = "192.168.1.116";
        //启动InChat
        InitServer.open();
    }

    @Override//为了打包springboot项目
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }

    public static void map(){
    }

    public static HashMap<String, Object> bean2Map(Object o) {

        Method[] declaredMethods = o.getClass().getDeclaredMethods();

        return bean2Map(o.toString());
    }

    public static HashMap<String, Object> bean2Map(String jsonString) {

        HashMap<String, Object> map = new HashMap<>();

//        String jsonString = gson.toJson(bean);
//

//        for (Method m : declaredMethods) {
//            if (m.getName().length() > 3 && m.getName().substring(0, 3).equals("get")) {
//                Class<?>[] getTypeParameters = m.getParameterTypes();
//                if(getTypeParameters.length==0){
//                    String substring = m.getName().substring(3, m.getName().length());
//                    String s = toLowerCaseFirstOne(substring);
//                    try {
//                        map.put(s,m.invoke(o));
//                        System.out.print("key:" + s + "======> " + m.invoke(o));
//                    } catch (IllegalAccessException | InvocationTargetException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }
        return null;
    }

    //首字母转小写
    private static String toLowerCaseFirstOne(String s){
        if(Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }

}
